#!/bin/sh -e
pprint ()
{
	TIMESTAMP=`date "+%d%m%y%H%M%S"`
	if [ "$VERBOSE" = "1" ]; then
		MYLOGFILE=$TMP/tmp_$TIMESTAMP.logfile
		echo "**********************************DINFO************************************" > $MYLOGFILE
		echo $* >> $MYLOGFILE
		echo "***************************************************************************" >> $MYLOGFILE
		cat $MYLOGFILE
		if [ ! "$LOGFILE" = "" ]; then
			cat $MYLOGFILE >> $LOGFILE
		fi
		rm -f $MYLOGFILE
	else
		echo $* >> /dev/null 2>&1
	fi
}
#------------------------------pecho------------------------------------
#if $LOGFILE exists, echo and write to logfile, else simple echo!
pecho ()
{
	TIMESTAMP=`date "+%d%m%y%H%M%S"`
	MYLOGFILE=$TMP/tmp_$TIMESTAMP.logfile
	echo $* > $MYLOGFILE
	cat $MYLOGFILE
	if [ ! "$LOGFILE" = "" ]; then
		cat $MYLOGFILE >> $LOGFILE
	fi
}

#-----------------------write file--------------------------------------
#----------------first argument must be the text------------------------
pwrite()
{
	echo $1 >> $2
}

#-------------------------set date--------------------------------------
psetdate()
{
	DATE=`date '+%d%m%y'`
	HOUR=`date '+%H'`
	MINUTE=`date '+%M'`
	SECOND=`date '+%S'`
	WEEKDAY=`date '+%a'`
}
#--------------------format an error description text----------------------
# takes two arguments, first of it being short description, second long description ....
# if LOGFILE env variable exists, the output will be written to that file too!
perr()
{
	TIMESTAMP=`date "+%d%m%y%H%M%S"`
	MYLOGFILE=$TMP/tmp_$TIMESTAMP.logfile
	echo "##############################################################################" > $MYLOGFILE
	echo "--------------------------------ERROR OCCURED---------------------------------" >> $MYLOGFILE
	echo "##############################################################################" >> $MYLOGFILE
	echo "******************************************************************************" >> $MYLOGFILE
	echo "----------------------Summary :-----------------------------------------------" >> $MYLOGFILE
	echo "******************************************************************************" >> $MYLOGFILE
	echo "*																			*" >> $MYLOGFILE
	echo "*" $1 >> $MYLOGFILE
	echo "*																			*" >> $MYLOGFILE
	if [ "$VERBOSE" = "1" ];then
	echo "******************************************************************************" >> $MYLOGFILE
	echo "----------------------Long description :--------------------------------------" >> $MYLOGFILE
	echo "******************************************************************************" >> $MYLOGFILE
	echo "*																			*" >> $MYLOGFILE
	if [ -z "$2" ]; then
		echo "* No Long Description available!" >> $MYLOGFILE
	else
		for number in "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" ; do
		if [ ! "$number" = "" ];then
			echo "*" $number >> $MYLOGFILE
		fi
		done
	fi
	echo "*																			*" >> $MYLOGFILE
	echo "******************************************************************************" >> $MYLOGFILE
	fi
	echo "##############################################################################" >> $MYLOGFILE
	echo "----------------------------END OF DESCRIPTION, EXITING ----------------------" >> $MYLOGFILE
	echo "##############################################################################" >> $MYLOGFILE
	cat $MYLOGFILE
	if [ ! "$LOGFILE" = "" ]; then
		cat $MYLOGFILE >> $LOGFILE
	fi 
	rm -f $MYLOGFILE
	exit 1
}
#########################################################################################
# Usage function #
##################
pusage() {
	echo "##########################################################################"
	echo "#                   USAGE of oss uploader                                #"
	echo "##########################################################################"
	echo "#                                                                        #"
	echo "# This script is used to upload artifacts to aliyun oss bucket.          #'
	echo "#                                                                        #"
	echo "##########################################################################"
	echo "#         MODES of OPERATION                                             #"
	echo "# -e ENDPOINT     ....    the target oss endpoint                        #"
	echo "# -s SOURCE REPO  ....    the path of source repo                        #"
	echo "# -t TARGET REPO  ....    the path of target repo                        #"
	echo "# -h              ....    print this usage                               #"
	echo "##########################################################################"
}
#################################
# Retrieve all possible options #
#################################
pgetopt(){
	case $1 in
		s)
			SOURCE="$OPTARG"
			;;
		t)
			TARGET="$OPTARG"
			;;
		e)
			ENDPOINT="$OPTARG"
			;;
		h)
			pusage
			exit 0
			;;
	esac
}
###############################
# Set up all needed variables #
###############################
psetup(){
	SHELLHOME=`dirname $0 | sed -e 's=/\.$==g'`
	if [ "$SHELLHOME" = "." ]; then
		SHELLHOME="$PWD"
	else
		SHELLHOME=`echo $SHELLHOME | sed -e "s=^\.=$PWD/.=g"`
	fi
	if [ "$TMP" = "" ]; then
		TMP=/tmp
	fi	      
	psetdate
	pprint "Shellscript started on: $DATE, time: $HOUR:$MINUTE:$SECOND"
	SHELLNAME=`basename $0 .sh`
	if [ "$MYTMP" = "" ]; then
		MYTMP=$TMP/"$SHELLNAME"_"$DATE"_"$HOUR"_"$MINUTE"_"$SECOND"
	fi	  
	if [ -d "$MYTMP" ]; then
		rm -rf $MYTMP
	fi	  
	mkdir $MYTMP
	REMEMBEREDDIR=`pwd`
}
######################################################
# Check options, create defaults for ungiven options #
######################################################
pcheckopts(){
	pprint "Checking Options!"
	if [ "$SOURCE" = "" -o "$TARGET" = "" -o "$ENDPOINT" = ""  ]; then
		perr "Required -e/-s/-t is missing."
	fi
    if [ "$ACCESS_KEY" = "" -o "$ACCESS_SECRET" = "" ]; then
		perr "Required env ACCESS_KEY or ACCESS_SECRET is missing."
	fi
}
######################################################
# Upload artifacts to oss                            #
######################################################
pupload(){
    pprint "Uploading $SOURCE to $ENDPOINT/$TARGET"
    ossutil cp --recursive -e "$ENDPOINT" \
	    -i "$ACCESS_KEY" -k "$ACCESS_SECRET" --force "$SOURCE" "$TARGET"
}
##################
# main program   #
##################
main(){
    psetup
	pcheckopts
    pupload
	exit 0
}
#########
# Do it #
#########
while getopts ":e:s:t:h" option
do
	case $option in
		\?)
			pusage
			echo "Incorrect usage!"
			echo "One or more arguments are not understood by this shell!"
			exit 1
			;;      
	esac
	pgetopt $option	pgetopt $option
done
main
